package com.dmcb.trade.business.crawlers.article;

import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 尚标网文章解析
 * Created by Administrator on 2016/6/20.
 */
@Service
public class ShangBiaoParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.SHANGBIAO_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "div.art h1";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.art>p";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.arttext";
    }

    /**
     * 获取封面选择器
     *s
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.arttext>div img[src]";
    }


    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        if (element != null) {
            String time =  text(element);
            time = time.substring(0,time.indexOf("浏览"));
            time = time.replace("发布时间：","").trim();
            SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = sdf.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (date != null) {
                article.setPostTime(date);
            }
        }
        super.setPostDate(doc, article);
    }
    @Override
    protected String platform() {
        return "商标网";
    }
}
